package com.kzd.gtgame.main.access

import android.text.method.LinkMovementMethod
import android.view.View
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.RegexUtils
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.custom.toAgreeLinks
import com.kzd.gtgame.jetpack.vm.AccessViewModel
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_access_register.*
import java.util.concurrent.TimeUnit

class AccessRegisterFragment : OwnerViewModeFragment<AccessViewModel>() {

    companion object {
        fun newInstance(): AccessRegisterFragment {
            return AccessRegisterFragment()
        }
    }

    override fun viewModel(): AccessViewModel {
        return createViewModel(requireActivity())
    }

    override val layoutResId: Int
        get() = R.layout.fragment_access_register

    private var mRegisterType: Int = 0

    override fun initView() {

        tvAccessRegisterPact.text = resources.getString(R.string.app_pact2).toAgreeLinks(activity = requireActivity())
        tvAccessRegisterPact.movementMethod = LinkMovementMethod.getInstance()


        etAccessRegisterAccount.addTextChangedListener {
            if (RegexUtils.isMobileExact(it)) {
                tvAccessRegisterSubmit.isEnabled = !it.isNullOrEmpty() &&
                        !etAccessRegisterCode.text.isNullOrEmpty() &&
                        !etAccessRegisterPassword.text.isNullOrEmpty()
                clAccessRegisterCode.visibility = View.VISIBLE
                tvAccessRegisterGetCode.isEnabled = true
                tvAccessRegisterGetCode.text = "获取验证码"
                mRegisterType = 1
            } else {
                tvAccessRegisterSubmit.isEnabled = !it.isNullOrEmpty() &&
                        !etAccessRegisterPassword.text.isNullOrEmpty()
                clAccessRegisterCode.visibility = View.GONE
                tvAccessRegisterGetCode.isEnabled = false
                mRegisterType = 0
            }
            etAccessRegisterCode.text = null
            disposable?.dispose()
        }

        etAccessRegisterCode.addTextChangedListener {
            if (RegexUtils.isMobileExact(etAccessRegisterAccount.text)) {
                tvAccessRegisterSubmit.isEnabled = !it.isNullOrEmpty() &&
                        !etAccessRegisterAccount.text.isNullOrEmpty() &&
                        !etAccessRegisterPassword.text.isNullOrEmpty()
            }
        }

        etAccessRegisterPassword.addTextChangedListener {
            if (RegexUtils.isMobileExact(etAccessRegisterAccount.text)) {
                tvAccessRegisterSubmit.isEnabled = !it.isNullOrEmpty() && it.length >= 6 &&
                        !etAccessRegisterAccount.text.isNullOrEmpty() &&
                        !etAccessRegisterCode.text.isNullOrEmpty()
            } else {
                tvAccessRegisterSubmit.isEnabled = !it.isNullOrEmpty() && it.length >= 6 &&
                        !etAccessRegisterAccount.text.isNullOrEmpty()
            }
        }

        tvAccessRegisterGetCode.setOnClickAntiShakeListener {
            viewModel.accessGetCode(etAccessRegisterAccount.text.toString(), type = 0)
        }

        tvAccessRegisterSubmit.setOnClickAntiShakeListener {
            viewModel.accessRegName(
                etAccessRegisterAccount.text.toString(),
                etAccessRegisterPassword.text.toString(),
                mRegisterType, etAccessRegisterCode.text.toString()
            )
        }

    }

//    private fun switch(type:Int){
//        if (type == 1) {
//            clAccessRegisterCode.visibility = View.VISIBLE
//            tvAccessRegisterGetCode.isEnabled = true
//            tvAccessRegisterGetCode.text = "获取验证码"
//        } else {
//            clAccessRegisterCode.visibility = View.GONE
//            tvAccessRegisterGetCode.isEnabled = false
//        }
//        etAccessRegisterCode.text = null
//        disposable?.dispose()
//    }

    override fun initViewMode() {
        viewModel.accessGetCodeLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            startCodeDownTime()
        })
    }

    private var isCodeDownTime = false
    private var disposable: Disposable? = null

    /**
     * 获取验证码倒计时
     * */
    fun startCodeDownTime() {
        disposable = Flowable.intervalRange((1), (60), (0), (1), TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .doOnNext {
                tvAccessRegisterGetCode.text = "剩${60 - it}秒"
            }
            .doOnSubscribe {
                isCodeDownTime = true
                tvAccessRegisterGetCode.isEnabled = false
            }
            .doOnComplete {
                isCodeDownTime = false
                tvAccessRegisterGetCode.text = "发送验证码"
            }
            .subscribe()
        viewModel.dispose.add(disposable)
    }
}